package com.controller;/**
 * @AUTHOR: 我是章璐锋憨憨
 * @Data:
 * @Version:
 */

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;

/**
 * @outhor RD
 * 2021/12/23
 */
public class BaseServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1. 拿到字节码对象
        Class<? extends BaseServlet> cls = this.getClass();
        // 2. 需要知道用户想调用什么方法
        String path = req.getRequestURI();
        String method = path.substring(path.lastIndexOf("/") + 1);
        try {
            Method m = cls.getDeclaredMethod(method, HttpServletRequest.class, HttpServletResponse.class);
            m.invoke(this, req, resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
